import asyncio # 让代码⽀持异步操作
from mcp import ClientSession # MCP 客⼾端会话管理
from contextlib import AsyncExitStack # 资源管理（确保客⼾端关闭时释放资源）

class MCPClient:
  def __init__(self):
    self.session = None
    self.exit_stack = AsyncExitStack()

  async def connect_to_mock_server(self):
    """模拟 MCP 服务器的连接（暂不连接真实服务器）"""
    print("✅ MCP 客⼾端已初始化，但未连接到服务器")

  async def chat_loop(self):
    """聊天循环，模拟⽤⼾输⼊并发送消息"""
    print("\nMCP 客⼾端已启动！输⼊ 'quit' 退出")
    while True:
      try:
        query = input("输⼊消息: ").strip()
        if query == "quit":
          break
        print(f"\n🤖 [Mock Response] 你说的是：{query}")
      except Exception as e:
        print(f"\n⚠️ 发⽣错误: {str(e)}")

async def cleanup(self):
  """清理资源"""
  await self.exit_stack.aclose()
  print("\n✅ MCP 客⼾端已关闭")

async def main():
  client = MCPClient()
  try:
    await client.connect_to_mock_server()
    await client.chat_loop()
  finally:
    await client.cleanup()

if __name__ == "__main__":
  asyncio.run(main())